<?php

namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Admin\Item_stock;
use zgldh\QiniuStorage\QiniuStorage;
use Input;

class Item_stockController extends Controller
{
    public function edit_stock(){
        $id = Input::get('id');
        $type = Input::get('type');
        if($type == 'reduce'){
            $result = Item_stock::where('id',$id) -> decrement('stock');
        }elseif($type == 'plus'){
            $result = Item_stock::where('id',$id) -> increment('stock');
        }elseif($type == 'edit'){
            $result = Item_stock::where('id',$id) -> update([
                'stock' => Input::get('stock')
            ]);
        }
        return $result ? '1' : '0';
    }

    public function edit_img(){
        $id = Input::get('id');
        $disk = QiniuStorage::disk('qiniu_item');
        $url = Item_stock::where('id',$id) -> value('item_img');
        if($disk->exists($url) == true){
            //删除上一张图片
            $result = $disk->delete($url);
            if($result == false){
                $data = array(
                    'status'	=>	'0',
                    'message'	=>	'删除原文件失败',
                );
                return response() -> json($data);
            }else{
                $result = Item_stock::where('id',$id) -> update([
                    'item_img' => Input::get('img')
                ]);
                session(['_url' => null]);
                return $result ? '1' : '0';
            }
        }else{
            $result = Item_stock::where('id',$id) -> update([
                'item_img' => Input::get('img')
            ]);
            return $result ? '1' : '0';
        }
    }

    public function add(){
        $data = Input::all();
        $data['attr_ids'] = explode(',',$data['attr_ids']);
        foreach($data['attr_ids'] as $k => $v){
            if($v == 0 || $v == '') unset($data['attr_ids'][$k]);
        }
        if(empty($data['attr_ids'])){
            return '0';
        }else{
            $data['attr_ids'] = implode(',',$data['attr_ids']);
            $res = Item_stock::where('item_id',$data['item_id']) -> where('item_attribute_ids',$data['attr_ids']) -> get() -> toArray();
            if($res){
                return '0';
            }else{
                $result = Item_stock::insert([
                    'item_id'            =>	$data['item_id'],
                    'item_attribute_ids' =>	$data['attr_ids'],
                    'item_img'           =>	$data['item_img'] ? $data['item_img'] : '',
                    'stock'              =>	$data['stock'],
                ]);
                session(['_url' => null]);
                return $result ? '1' : '0';
            }
        }
    }

    public function del(){
        $id = Input::get('id');
        $disk = QiniuStorage::disk('qiniu_item');
        $url = Item_stock::where('id',$id) -> value('item_img');
        if($disk->exists($url) == true){
            //删除上一张图片
            $result = $disk->delete($url);
            if($result == false){
                $data = array(
                    'status'	=>	'0',
                    'message'	=>	'删除原文件失败',
                );
                return response() -> json($data);
            }else{
                $result = Item_stock::where('id',$id) -> delete();
                return $result ? '1' : '0';
            }
        }else{
            $result = Item_stock::where('id',$id) -> delete();
            return $result ? '1' : '0';
        }
    }
}